#!/usr/bin/env python


import os
import time
import shutil 
import csv

from post_process import *

def create_dir(config_name, number = 0):
	timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
	if number != 0:
		timestamp = timestamp + "_" + str(number)
	if config_name == "None":
		logdir = "log/%s/" % (timestamp,)
	else:
		logdir = "log/%s/%s/" % (config_name, timestamp,)
	os.popen("mkdir -p %s" % logdir)
	os.popen("cp quapi.conf %s" % logdir)
	return logdir

def write_log(logdir, results):
	fp = file("%s/data.csv" % logdir, "w")
	writer = csv.writer(fp)
	writer.writerows(results)

def write_info(logdir, results):
	max_ratio = results[0]
	max_ratio_time = results[1]
	max_up_population = results[2]
	max_up_population_time = results[3]
	min_decoherence = results[4]
	min_decoherence_time = results[5]
	fp = open(logdir + "/info.txt", "w")
	fp.write("max_ratio:" + str(max_ratio) + "\n")
	fp.write("max_ratio_time:" + str(max_ratio_time) + "\n")
	fp.write("max_up_population:" + str(max_up_population) + "\n")
	fp.write("max_up_population_time:" + str(max_up_population_time) + "\n")
	fp.write("min_decoherence:" + str(min_decoherence) + "\n")
	fp.write("min_decoherence_time:" + str(min_decoherence_time) + "\n")
	fp.close()

def write_statics(logdir, results):
	results.insert(0, ["time", "spin_up_prob", "spin_down_prob", "thermal_average", "decoherence", "ratio"])
	fp = file("log/%s/statics.csv" % logdir, "w")
	writer = csv.writer(fp)
	writer.writerows(results)
	
	
